package rlb;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import rlb.test.RobotTestBed;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * FlankBot Flank Test.
 * 
 * @author Remy Baumgarten
 * 
 */
public class TestFlankBotFlanks extends RobotTestBed {

  private int flanked = 0;
  private int turn = 0;

  @Override
  public String getRobotNames() {
    return "sample.Walls,rlb.FlankBot";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Test to see that we flank to the left of the other bot.
   * 
   * @param event Details about the current battle
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // snapshots of both robots
    IRobotSnapshot myRobot = event.getTurnSnapshot().getRobots()[1];
    IRobotSnapshot otherRobot = event.getTurnSnapshot().getRobots()[0];
    double otherX = otherRobot.getX();
    double myX = myRobot.getX();
    // is flankbot to the left of otherbot?
    if (myX < otherX) {
      flanked++;
    }
    turn++;
  }

  /**
   * The actual test, which asserts that FlankBot actually Flanks 1/4 of total time.
   * 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    System.out.println(flanked);
    // Return the results in order of getRobotNames.
    BattleResults[] results = event.getIndexedResults();
    // Sanity check that results[1] is FlankBot (not strictly necessary, but illustrative).
    assertEquals("Check results[1]", results[1].getTeamLeaderName(), "rlb.FlankBot");

    // Check to make sure FlankBot is Flanking to the Left a quarter of the time
    assertTrue("Checking to see if flanked over 10 times", (flanked > (turn / 4)));
  }
}